﻿using DL.Initializers;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DL.Repositories
{
    public class CauHinhRepository
    {
        private SqlConnection _connect;
        private SqlDataAdapter _sqlAdapter;
        public DataTable ChiTietCauHinh()
        {
            try
            {
                ConnectDataBase.CloseConnection(_connect);
                _connect = new SqlConnection();
                _connect = ConnectDataBase.GetSQlConnection(_connect);
                var cm = _connect.CreateCommand();
                cm.CommandText = "dbo.C_DANHSACH_CAUHINH";
                cm.CommandType = CommandType.StoredProcedure;
                var tableGs = new DataTable();
                _sqlAdapter = new SqlDataAdapter(cm);
                _sqlAdapter.Fill(tableGs);
                return tableGs;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                ConnectDataBase.CloseConnection(_connect);
            }
        }
        public DataTable DanhSachSlide()
        {
            try
            {
                ConnectDataBase.CloseConnection(_connect);
                _connect = new SqlConnection();
                _connect = ConnectDataBase.GetSQlConnection(_connect);
                var cm = _connect.CreateCommand();
                cm.CommandText = "dbo.C_DANHSACH_SLIDE";
                cm.CommandType = CommandType.StoredProcedure;
                var tableGs = new DataTable();
                _sqlAdapter = new SqlDataAdapter(cm);
                _sqlAdapter.Fill(tableGs);
                return tableGs;
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                ConnectDataBase.CloseConnection(_connect);
            }
        }

        public string CapNhatSlide(Slide sl)
        {
            try
            {

                if (sl.Id != 0)
                {

                    ConnectDataBase.CloseConnection(_connect);
                    _connect = new SqlConnection();
                    _connect = ConnectDataBase.GetSQlConnection(_connect);
                    var cm = _connect.CreateCommand();
                    cm.CommandText = "dbo.C_DELETE_SLIDE";
                    cm.CommandType = CommandType.StoredProcedure;
                    cm.Parameters.Add("@id", SqlDbType.BigInt).Value = sl.Id;
                    cm.Parameters.Add("@NewQuoteNumber", SqlDbType.Int).Direction = ParameterDirection.Output;
                    //cm.Parameters.Add(newReqNumber);
                    cm.ExecuteNonQuery();
                    string NewQuoteNumber = cm.Parameters["@NewQuoteNumber"].Value.ToString();
                    if (NewQuoteNumber == "0" || NewQuoteNumber == "") throw new Exception();
                }
                else
                {
                    //them moi
                    ConnectDataBase.CloseConnection(_connect);
                    _connect = new SqlConnection();
                    _connect = ConnectDataBase.GetSQlConnection(_connect);
                    var cm = _connect.CreateCommand();
                    cm.CommandText = "dbo.C_INSERT_SLIDE";
                    cm.CommandType = CommandType.StoredProcedure;
                    cm.Parameters.Add("@duong_dan", SqlDbType.NVarChar).Value = sl.DuongDan;
                    cm.Parameters.Add("@thu_tu", SqlDbType.BigInt).Value = sl.ThuTu;
                    cm.Parameters.Add("@trang_thai", SqlDbType.Int).Value = 0;
                    cm.Parameters.Add("@NewQuoteNumber", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                    cm.ExecuteNonQuery();
                    string NewQuoteNumber = cm.Parameters["@NewQuoteNumber"].Value.ToString();
                    if (NewQuoteNumber == "0" || NewQuoteNumber == "") throw new Exception();
                }
                return "Thành công";
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                ConnectDataBase.CloseConnection(_connect);
            }
        }

        public string CapNhatCauHinh(CauHinh ch)
        {
            try
            {

                
                    //them moi
                    ConnectDataBase.CloseConnection(_connect);
                    _connect = new SqlConnection();
                    _connect = ConnectDataBase.GetSQlConnection(_connect);
                    var cm = _connect.CreateCommand();
                    cm.CommandText = "dbo.C_UPDATE_CAUHINH";
                    cm.CommandType = CommandType.StoredProcedure;
                    cm.Parameters.Add("@id", SqlDbType.NVarChar).Value = ch.Id;
                    cm.Parameters.Add("@imagelogo", SqlDbType.NVarChar).Value = ch.Image2;
                    cm.Parameters.Add("@imagehd", SqlDbType.NVarChar).Value = ch.Image1;
                    cm.Parameters.Add("@titlemenu", SqlDbType.NVarChar).Value = ch.Text1;
                    cm.Parameters.Add("@titlechitiet", SqlDbType.NVarChar).Value = ch.Text3;
                    cm.Parameters.Add("@title", SqlDbType.NVarChar).Value = ch.Text2;
                    cm.Parameters.Add("@huongdan", SqlDbType.NVarChar).Value = ch.Text4;
                    cm.Parameters.Add("@mamau", SqlDbType.NVarChar).Value = ch.Text5;
                    cm.Parameters.Add("@NewQuoteNumber", SqlDbType.BigInt).Direction = ParameterDirection.Output;
                    cm.ExecuteNonQuery();
                    string NewQuoteNumber = cm.Parameters["@NewQuoteNumber"].Value.ToString();
                    if (NewQuoteNumber == "0" || NewQuoteNumber == "") throw new Exception();
                    return "Thành công";
            }
            catch (Exception)
            {
                return null;
            }
            finally
            {
                ConnectDataBase.CloseConnection(_connect);
            }
        }
    }
}
